package com.satan.elasticsearch.service.impl;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.DeleteRequest;
import co.elastic.clients.elasticsearch.core.IndexRequest;
import com.satan.elasticsearch.dao.entity.Hotel;
import com.satan.elasticsearch.doc.HotelDoc;
import com.satan.elasticsearch.dto.resp.RestResp;
import com.satan.elasticsearch.mapper.HotelMapper;
import com.satan.elasticsearch.service.HotelEsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Author: Demon
 * @Date: 2024/2/19 00:45
 * @Description:
 **/
@Service
@RequiredArgsConstructor
public class HotelEsServiceImpl implements HotelEsService {
    private final ElasticsearchClient esClient;
    private final HotelMapper hotelMapper;
    @Override
    public RestResp saveHotelById(long id) throws IOException {
        // 0.根据id查询酒店数据
        Hotel hotel = hotelMapper.selectById(id);
        HotelDoc hotelDoc = new HotelDoc(hotel);
        IndexRequest<HotelDoc> request = IndexRequest.of(a -> a.index("hotel2").id(String.valueOf(id)).document(hotelDoc));
        esClient.index(request);
        return RestResp.ok();
    }

    @Override
    public RestResp deleteHotelById(long id) throws IOException {
        DeleteRequest request = DeleteRequest.of(a -> a.index("hotel2").id(String.valueOf(id)));
        esClient.delete(request);
        return RestResp.ok();
    }

}
